package com.jf.service.impl;

import com.jf.bean.Kaptcha;
import com.jf.bean.Token;
import com.jf.bean.Userinfo;
import com.jf.dao.TokenDao;
import com.jf.dao.UserinfoDao;
import com.jf.dao.impl.TokenDaoImpl;
import com.jf.dao.impl.UserinfoDaoImpl;
import com.jf.form.LoginForm;
import com.jf.service.KaptchaService;
import com.jf.service.TokenService;
import com.jf.service.UserinfoService;
import com.jf.util.MyConstants;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;
import com.jf.util.ResultJson;

import java.util.*;

public class UserinfoServiceImpl implements UserinfoService {
    private UserinfoDao userinfoDao = new UserinfoDaoImpl();
    private KaptchaService kaptchaService = new KaptchaServiceImpl();
    private TokenService tokenService = new TokenServiceImpl();


    @Override
    public ResultJson login(LoginForm loginForm) {
        /*检查验证码*/
        Kaptcha kaptcha = new Kaptcha();
        kaptcha.setUuid(loginForm.getUuid());
        kaptcha.setText(loginForm.getCode());
        if(!kaptchaService.checkKaptcha(kaptcha)){
            return ResultJson.error("验证码错误！");
        }
        /*检查用户登录是否成功*/
        Userinfo userinfo = userinfoDao.getByUsername(loginForm.getUsername());
        if(userinfo == null || !userinfo.getPassword().equals(MyUtils.MD5(loginForm.getPassword()))){
            return ResultJson.error("用户名或密码不正确！");
        }
        /*检查状态*/
        if(userinfo.getStatus() != 1){
            return ResultJson.error("该用户已被封禁");
        }
        /*获取token*/
        String token = tokenService.getToken(userinfo.getId());
        Map<String,String> map = new HashMap<>();
        map.put("token",token);
        return ResultJson.success(map);
    }

    @Override
    public ResultJson getPage(PageInfo pageInfo, Userinfo userinfo) {
        List<Userinfo> list = userinfoDao.getPage(pageInfo,userinfo);
        int count = userinfoDao.getCount(userinfo);
        pageInfo.setPageData(list,count);
        return ResultJson.success(pageInfo);
    }

    @Override
    public ResultJson saveOrUpdate(Userinfo userinfo) {
        int result;
        if(userinfo.getId() == null){
            Userinfo searchInfo = userinfoDao.getByUsername(userinfo.getUsername());
            if(searchInfo == null){
                userinfo.setPassword(MyUtils.MD5(MyConstants.DEFAULT_PASSWORD));
                result = userinfoDao.add(userinfo);
            }else{
                return ResultJson.success("用户名已存在!");
            }
        }else{
            if(MyUtils.isStringNotEmpty(userinfo.getPassword())){
                userinfo.setPassword(MyUtils.MD5(userinfo.getPassword()));
            }
            if(userinfo.getStatus() != null && userinfo.getId() == MyConstants.ADMIN_ID){
                return ResultJson.error("管理员禁止修改状态！");
            }
            result = userinfoDao.updateSelect(userinfo);
        }
        return result>0? ResultJson.success() : ResultJson.error("操作失败！");
    }

    @Override
    public ResultJson delete(int id) {
        if(id == MyConstants.ADMIN_ID){
            return ResultJson.error("管理员禁止删除");
        }
        int result = userinfoDao.delete(id);
        return result>0? ResultJson.success() : ResultJson.error("操作失败！");
    }



    @Override
    public ResultJson checkPassword(Userinfo userinfo) {
        Userinfo searchInfo = userinfoDao.getById(userinfo.getId());
        if(searchInfo != null && searchInfo.getPassword().equals(MyUtils.MD5(userinfo.getPassword()))){
            return ResultJson.success();
        }
        return ResultJson.error("原密码不正确！");
    }

    @Override
    public ResultJson batchDelete(String idStr) {
        if(MyUtils.isStringEmpty(idStr)){
            return ResultJson.error("编号异常，无法删除！");
        }
        int[] ids = MyUtils.string2IntArray(idStr);
        /*先排序，再进行二分查找*/
        Arrays.sort(ids);
        int index = Arrays.binarySearch(ids,MyConstants.ADMIN_ID);
        if(index >= 0){
            return ResultJson.error("管理员禁止删除！");
        }
        int result = userinfoDao.batchDelete(ids);
        return result == ids.length?ResultJson.success():ResultJson.error("删除异常");
    }

    @Override
    public Userinfo getById(int id) {
        return userinfoDao.getById(id);
    }
}
